package com.turing.usdtpay.utils;

import com.turing.usdtpay.entity.LoginData;
import com.turing.usdtpay.manager.UserManager;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * @作者：JAY
 * @创建时间：2019/5/13 13:41
 * @描述：
 **/
public class SortUtils {
    private static String KEY = "key=";

    public String sortParams(HashMap<String ,String> paramMap){

        StringBuilder builder = new StringBuilder();
        StringBuilder before = new StringBuilder();
        StringBuilder after = new StringBuilder();
        String[] strings = new String[paramMap.size()];
        int count = 0;
        if (paramMap.size()>0){
            for (Map.Entry<String,String> entry :paramMap.entrySet()){
                strings[count] = entry.getKey();
                count++;
            }

            for (String s:strings){
                before = before.append(s+",");
            }

            Arrays.sort(strings,new ComparatorByDate());

            for (String s:strings){
                after = after.append(s+",");
            }
//            LogUtils.d( " keyList sort after-->"+after.toString());
            for (int i= 0; i< strings.length;i++){
                builder = builder.append(strings[i]+"="+paramMap.get(strings[i])+"&");
            }
//            LogUtils.d("sort after -->"+builder+KEY+getKey());
            return builder+KEY+getKey();
        }
        return "";
    }

    private String getKey(){
        LoginData loginDate = UserManager.getInstance().getLoginDate();
        String trader = loginDate.getTrader();
        String verify = loginDate.getVerify();
        String s = verify.substring(24, 32);
        String s1 = verify.substring(0, 4);
        String string = s+trader+s1;
//        LogUtils.d("key -->"+string);
        return EncryptUtils.encrypt32BitMD5(string);
    }

    /**
     * 比较两者的字母顺序
     */
    private class ComparatorByDate implements Comparator<String>{

        @Override
        public int compare(String o1, String o2) {
            if (o1.length() > o2.length()) {
                for (int i = 0; i < o2.length(); i++) {
                    if (o1.getBytes()[i] != o2.getBytes()[i]) {
                        return o1.getBytes()[i] > o2.getBytes()[i] ? 1 : -1;
                    }
                    if (i == o1.length() - 1) {
                        if (o1.getBytes()[o2.length() - 1] == o2.getBytes()[o2.length() - 1]) {
                            return -1;
                        }
                    }
                }
            }else {
                for (int i = 0; i< o1.length();i++){
                    if (o1.getBytes()[i] != o2.getBytes()[i]){
                        return o1.getBytes()[i] > o2.getBytes()[i]? 1:-1;
                    }
                    if (i == o1.length()-1){
                        if (o1.getBytes()[o1.length()-1] == o2.getBytes()[o1.length()-1]){
                            return -1;
                        }
                    }
                }
            }

            return 0;
        }
    }


}
